<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="basePath" scope="session" value="${pageContext.request.contextPath}"></c:set>

<div style="margin-left: 20px;">
<c:forEach items="${operationList}" var="operation">
	<c:if test="${operation.check}">
	 <div class="checkbox">
		<label>
			<input name="form-field-checkbox" type="checkbox" value="${operation.id}" checked="checked" class="ace">
			<span class="lbl">${operation.name}</span>
		</label>
	</div>
	 </c:if>
 	<c:if test="${!operation.check}">
	 <div class="checkbox">
		<label>
			<input name="form-field-checkbox" type="checkbox" value="${operation.id}" class="ace">
			<span class="lbl">${operation.name}</span>
		</label>
	 </div>
	 </c:if> 
	<br>
</c:forEach>
</div>
<script type="text/javascript">
$("input[name='form-field-checkbox']").change(function(){
	if($(this).next()[0].innerHTML == "所有"){
		if($(this)[0].checked){
			$("input[name='form-field-checkbox']").each(function(i){
			   this.checked = true;
		 	});
	 	}else{
		 	$("input[name='form-field-checkbox']").each(function(i){
			   this.checked = false;
		 	});
	 	}
	}else{
		if(!$(this)[0].checked){
			$("input[name='form-field-checkbox']:first-child")[0].checked=false;
	 	}else{
	 	 	var k = 0;
	 		$("input[name='form-field-checkbox']").each(function(i){
			   if($(this).next()[0].innerHTML != "所有" &&!this.checked){
			   	 k++;
			   }
		 	});
	 	   if(k == 0){
		   	$("input[name='form-field-checkbox']:first-child")[0].checked=true;
		   }
	 	}
	}
});


function submitOperation() {
	var resourceId = "${resourceId}";
	var node = $('#resourceid').tree('find', resourceId);
	console.info(node);  
	if(!node.checked){
		alertTip('请先选中资源:->'+node.text,'提示');
		return;
	} 
	//$('#resourceid').tree('check', node.target);  
	var roleId = $("#rid").val();
	var permissionIds = "";
	$("input[name='form-field-checkbox']").each(function(i){
		   if(this.checked){
			   permissionIds+=this.value+",";
		   }
	 });
	permissionIds=escape(permissionIds); 
	doSubmit("${basePath}/admin/role/updatePermission?resourceId=" + resourceId + "&roleId=" + roleId+"&permissionIds="+permissionIds);
}
</script>
